package com.zkca.xf.presenter;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;
import com.zkca.xf.base.BaseFragmentPresenter;
import com.zkca.xf.base.BasePresenter;
import com.zkca.xf.manager.DataManager;
import com.zkca.xf.model.javabean.upHead;
import com.zkca.xf.view.UpHeadView;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;

/**
 * Created by li on 2018/8/9.
 */

public class UpHeadPresenter extends BasePresenter {
    private DataManager dataManager;
    private UpHeadView upHeadView;
    public UpHeadPresenter(LifecycleProvider<ActivityEvent> provider , UpHeadView upHeadView) {
        super(provider);
        dataManager = DataManager.getInstance();
        this.upHeadView=upHeadView;
    }
    public void upHead(List<MultipartBody.Part> partList){

        dataManager.UpHead(partList).
                subscribeOn(Schedulers.io())// 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread())// UI线程处理返回接口
                .compose(getProvider().<upHead>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<upHead>() {// 订阅
                    @Override
                    public void onNext(upHead s) {
                        upHeadView.getSelectServer(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        upHeadView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        upHeadView.hideProgressDialog();
                    }
                });

    }
}
